package yass.drawcard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.core.utils.dbtutil.PrefUtils;

import yass.drawcard.utils.ShanHaiUtil;

public class MineActivity extends AppCompatActivity implements View.OnClickListener , RadioGroup.OnCheckedChangeListener{


    private TextView httpipTv;
    private TextView remoteBtn;
    private RadioGroup servicepathRg;
    private RadioButton homeCb;
    private RadioButton companyCb;
    private TextView thinkpadBtn;
    private TextView pcBtn1;
    private TextView pcBtn2;
    private EditText editEt;
    private Button editBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);

        // 初始化视图
        initView();
        // 初始化数据
        initData();
    }

    private void initView() {

        httpipTv = (TextView) findViewById(R.id.httpip_tv_current);

        servicepathRg = (RadioGroup) findViewById(R.id.httpip_rg_servicepath);
        homeCb = (RadioButton) findViewById(R.id.httpip_cb_home);
        companyCb = (RadioButton) findViewById(R.id.httpip_cb_company);

        remoteBtn = (TextView) findViewById(R.id.httpip_btn_remote);
        thinkpadBtn = (TextView) findViewById(R.id.httpip_btn_thinkpad);
        pcBtn1 = (TextView) findViewById(R.id.httpip_btn_pc1);
        pcBtn2 = (TextView) findViewById(R.id.httpip_btn_pc2);
        editEt = (EditText) findViewById(R.id.httpip_et_edit);
        editBtn = (Button) findViewById(R.id.httpip_btn_edit);

        servicepathRg.setOnCheckedChangeListener(this);
        remoteBtn.setOnClickListener(this);
        thinkpadBtn.setOnClickListener(this);
        pcBtn1.setOnClickListener(this);
        pcBtn2.setOnClickListener(this);
        editBtn.setOnClickListener(this);

    }

    private void initData() {

        String httpid = ShanHaiUtil.getHttpid();
        httpipTv.setText(httpid);

        String servicepath = PrefUtils.getString(MineActivity.this,"servicepath","company");
        if("home".equals(servicepath)){
            homeCb.setChecked(true);
        }else{
            companyCb.setChecked(true);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.httpip_btn_remote://
                httpipTv.setText(remoteBtn.getText().toString());
                editEt.setText(remoteBtn.getText().toString());
                ShanHaiUtil.putHttpid(remoteBtn.getText().toString());
                break;
            case R.id.httpip_btn_thinkpad://
                httpipTv.setText(thinkpadBtn.getText().toString());
                editEt.setText(thinkpadBtn.getText().toString());
                ShanHaiUtil.putHttpid(thinkpadBtn.getText().toString());
                break;
            case R.id.httpip_btn_pc1://
                httpipTv.setText(pcBtn1.getText().toString());
                editEt.setText(pcBtn1.getText().toString());
                ShanHaiUtil.putHttpid(pcBtn1.getText().toString());
                break;
            case R.id.httpip_btn_pc2://
                httpipTv.setText(pcBtn2.getText().toString());
                editEt.setText(pcBtn2.getText().toString());
                ShanHaiUtil.putHttpid(pcBtn2.getText().toString());
                break;
            case R.id.httpip_btn_edit://
                String httpip = editEt.getText().toString();
                if (TextUtils.isEmpty(httpip)) {
                    Toast.makeText(MineActivity.this,"请先设置ID",Toast.LENGTH_SHORT).show();
                } else {
                    ShanHaiUtil.putHttpid(httpip);


                    Intent intent = new Intent();
                    // intent.putExtra("poiStc", poiStc);
                    // 设置返回码和返回携带的数据
                    setResult(Activity.RESULT_OK, intent);

                    MineActivity.this.finish();
                }
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {

            case R.id.httpip_cb_home://
                PrefUtils.putString(MineActivity.this,"servicepath","home");
                break;
            case R.id.httpip_cb_company://
                PrefUtils.putString(MineActivity.this,"servicepath","company");
                break;

        }
    }


}
